import {
  selectDailyAppUserStatistic,
  selectDailyOrderNum,
  selectMonthlyAppUserStatistic,
} from '@/api/userStatistics'

import dayjs from 'dayjs'
import { commonSettings } from './chargeQuery'

// 每日用户统计
export const handleDayUserData = (params, instance, callback) => {
  selectDailyAppUserStatistic(params)
    .then((res) => {
      commonSettings(
        {
          xAxisType: 'day',
          keys: [
            {
              value: 'userCount',
              yAxisName: '总用户数',
              seriesName: '总用户数',
            },
            {
              value: 'newUser',
              yAxisName: '新增用户数',
              seriesName: '新增用户数',
            },
          ],
          instance,
          xAxisFormat: (item) => {
            return dayjs(item.createTime).format('YYYY.MM.DD')
          },
        },
        res
      )
    })
    .catch((err) => {
      console.log(err)
    })
    .finally(() => {
      callback()
    })
}

// 每月用户统计
export const handleMonthUserData = (params, instance, callback) => {
  selectMonthlyAppUserStatistic(params)
    .then((res) => {
      commonSettings(
        {
          xAxisType: 'day',
          keys: [
            {
              value: 'userCount',
              yAxisName: '总用户数',
              seriesName: '总用户数',
            },
            {
              value: 'newUser',
              yAxisName: '新增用户数',
              seriesName: '新增用户数',
            },
          ],
          instance,
          xAxisFormat: (item) => {
            return dayjs(item.createTime).format('YYYY.MM')
          },
        },
        res
      )
    })
    .catch((err) => {
      console.log(err)
    })
    .finally(() => {
      callback()
    })
}

// 用户充电量趋势
export const handleChargeData = (params, instance, callback) => {
  selectDailyOrderNum(params)
    .then((res) => {
      commonSettings(
        {
          xAxisFormat: (item) => {
            return item.hour + '点'
          },
          keys: [
            {
              value: 'billCount',
              yAxisName: '订单数(个)',
              seriesName: '订单数(个)',
            },
          ],
          instance,
        },
        res
      )
    })
    .catch((err) => {
      console.log(err)
    })
    .finally(() => {
      callback()
    })
}
